package com.cari.oa.controller;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.cari.oa.domain.NewsEntity;
import com.cari.oa.domain.SysUserEntity;
import com.cari.oa.service.NewsService;
import com.cari.oa.service.SysUserService;
import com.cari.oa.util.StrUtils;
import com.cari.oa.util.Utils;

/**
 * 个人信息修改controller
 * User: yexuhui
 * Date: 12-12-17
 * Time: 上午11:08
 */
@Controller
@SessionAttributes("username")
public class PersonalController {

    @Resource
    private SysUserService sysUserService;
    
    @Resource
	private NewsService newsService;
    public static final String MODULE_NAME = "SELF_MANAGE";

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }

    @RequestMapping("/personal/welcome.do")
    public String welcome(ModelMap modelMap) {
    	List<NewsEntity> newsList = newsService.getPage(null, 1, 5);
    	modelMap.addAttribute("newsList", newsList);
    	
        return "personal/welcome";
    }

    @RequestMapping("/personal/selfview.do")
    public String selfview(@ModelAttribute("username") String username, ModelMap modelMap) {
        SysUserEntity userEntity = sysUserService.getByEnname(username);
        modelMap.addAttribute("user", userEntity);
        return "personal/self_view";
    }

    @RequestMapping(value = "/personal/selfedit.do", method = RequestMethod.GET)
    public String enterSelfEdit(@ModelAttribute("username") String username, ModelMap modelMap) {
        SysUserEntity userEntity = sysUserService.getByEnname(username);
        modelMap.addAttribute("user", userEntity);
        return "/personal/self_edit";
    }

    @RequestMapping(value = "/personal/selfedit.do", method = RequestMethod.POST)
    @ResponseBody
    public String postSelfEdit(@ModelAttribute("username") String username, SysUserEntity userEntity) {
        SysUserEntity originUser = sysUserService.getByEnname(userEntity.getEnname());
        userEntity.setId(originUser.getId());
        userEntity.setPassword(originUser.getPassword());
        userEntity.setStatus(originUser.getStatus());
        userEntity.setCreateTime(originUser.getCreateTime());

        ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
        Validator validator = validatorFactory.getValidator();
        Set<ConstraintViolation<SysUserEntity>> violations = validator.validate(userEntity);
        if(violations.size() != 0) {
            for (ConstraintViolation<SysUserEntity> violation : violations) {
                return Utils.errorJsonResp(violation.getMessage());
            }
        }

        sysUserService.update(userEntity);
        
        SysOperLogController.addOperLog(username, MODULE_NAME, "修改个人资料");
        return Utils.normalJsonResp();
    }

    @RequestMapping(value = "/personal/pass.do", method = RequestMethod.GET)
    public String enterPass() {
        return "/personal/pass";
    }

    @RequestMapping(value = "/personal/pass.do", method = RequestMethod.POST)
    @ResponseBody
    public String postPass(@ModelAttribute("username") String username, String oldPassword,
                           String password1, String password2) {
        if(StrUtils.isEmpty(oldPassword)) return Utils.errorJsonResp("旧密码不能为空");
        if(StrUtils.isEmpty(password1)) return Utils.errorJsonResp("新密码不能为空");
        if(StrUtils.isEmpty(password2)) return Utils.errorJsonResp("确认密码不能为空");

        if(sysUserService.getByNameAndPass(username, oldPassword) == null)
            return Utils.errorJsonResp("旧密码不正确");

        if(!password1.equals(password2)) return Utils.errorJsonResp("两次输入的密码不匹配");

        sysUserService.changePassword(username, password1);
        
        SysOperLogController.addOperLog(username, MODULE_NAME, "修改个人密码");
        return Utils.normalJsonResp();
    }
}
